#include "pb_base.h"

void TestBase::Test()
{
	std::string data;
	SaveData(data);
	LoadData(data);
}

void TestBase::SaveData(std::string& data)
{
	cs::expression expr;
	expr.set_id(1);
	for (int i = 1; i <= 2; i++)
	{
		cs::unit* u = expr.add_list();
		if (nullptr == u)
		{
			continue;
		}
		u->set_type(cs::UNIT_DIGIT);
		u->set_digit(i + 1);

	}

	cs::unit* u = expr.add_list();
	u->set_type(cs::UNIT_OPER);
	u->set_operation("add");

	cs::Entity e;
	e.set_id(100);
	e.set_name("boss");

	cs::Scene s;
	
	AppendMessage(data, s);
	AppendMessage(data, e);
	AppendMessage(data, expr);
}

void TestBase::LoadData(std::string& data)
{
	std::string name;
	std::string msg;
	while (PickMessage(data, name, msg))
	{
		if (name == "cs.Scene")
		{
			cs::Scene s;
			s.ParseFromString(msg);
			std::cout << s.DebugString() << std::endl;
		}
		else if (name == "cs.Entity")
		{
			cs::Entity e;
			e.ParseFromString(msg);
			std::cout << e.DebugString() << std::endl;

			// test before after compatible
			cs::EntityOld eOld;
			eOld.ParseFromString(msg);
			std::cout << "old protcol object " << eOld.DebugString() << std::endl;

			cs::EntityNew eNew;
			eNew.ParseFromString(msg);
			std::cout << "new protcol object " << eNew.DebugString() << std::endl;
		}
		else if (name == "cs.expression")
		{
			cs::expression expr;
			expr.ParseFromString(msg);
			std::cout << expr.DebugString() << std::endl;
		}
	}
}
